package com.aaa.qy127.dao.impl;

import com.aaa.qy127.dao.DingDanDao;
import com.aaa.qy127.entity.DingDan;
import com.aaa.qy127.util.JDBCUtil;
import jdk.nashorn.internal.scripts.JD;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository("dingDanDaoImpl")
public class DingDanDaoImpl implements DingDanDao {
    /**
     * 向 订单详情表中 添加一条数据
     * @param count
     * @param outprice
     * @param userid
     * @return
     */
    @Override
    public int insertOneDingDan(String count, String outprice,Integer userid) {
        Date date = new Date();
        //获取当前时间
        long time = date.getTime();
        //生成一个 六位数 的 随机数
        int i2 = (int) ((Math.random() * 9 + 1) * 100000);
        String s = String.valueOf(i2);
        String ddbh = time + s;

        int fuhao = outprice.lastIndexOf("￥");
        int xiaoshu = outprice.lastIndexOf(".");
        String substring = outprice.substring(fuhao + 1,xiaoshu);
        double sPrice = Integer.parseInt(substring);
        int sCount = Integer.parseInt(count);
        double zongjia = sPrice * sCount;
        String sql = "insert into t_dd values(null,?,?,?,?,?,0)";
        int i = JDBCUtil.getPrimaryKey(sql, date, userid, zongjia, count, ddbh);
        return i;
    }

    /**
     * 向 订单详情 表 中 添加 订单详情
     * @param bookid
     * @param count
     * @param outprice
     * @param ddid
     * @return
     */
    @Override
    public int insertDingDanXQ(String bookid, String count, String outprice, Integer ddid) {
        String sql = "insert into t_dd_xq values(null,?,?,?,?)";
        int fuhao = outprice.lastIndexOf("￥");
        String substring = outprice.substring(fuhao + 1);
        int i = JDBCUtil.update(sql, bookid, count, ddid, substring);
        return i;
    }


    /**
     * 查询所有订单信息和搜索
     * @param dingDan
     * @return
     */
    @Override
    public List<Map<String, Object>> queryAlldd(DingDan dingDan) {
        StringBuilder sql = new StringBuilder("select d.ddid,d.ddbh,d.ddate,u.name,d.totalprice,d.totalcount,u.address,d.status from t_dd d\n" +
                "JOIN t_user u\n" +
                "on d.userid = u.userid  where 1=1");
        if (dingDan.getName()!=null && !"".equals(dingDan.getName())){
            sql.append(" and u.name like '%").append(dingDan.getName()).append("%'");
        }
        if (dingDan.getDdbh()!=null && !"".equals(dingDan.getDdbh())){
            sql.append(" and d.ddbh like '%").append(dingDan.getDdbh()).append("%'");
        }
        if (dingDan.getStatus()!=null && !"".equals(dingDan.getStatus())){
            sql.append(" and d.status =").append(dingDan.getStatus());
        }
        if (dingDan.getOnTime()!=null && !"".equals(dingDan.getOnTime())){
            sql.append(" and d.ddate >= '").append(dingDan.getOnTime()).append("'");
        }
        if (dingDan.getOffTime()!=null && !"".equals(dingDan.getOffTime())){
            sql.append(" and d.ddate <= '").append(dingDan.getOffTime()).append("'");
        }
        sql.append(" limit ?,?");
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql.toString(),(dingDan.getPage()-1)*dingDan.getLimit(),dingDan
                .getLimit());
        if(maps.size()>0){
            return maps;
        }
        return null;
    }

    /**
     * 查询订单条数
     * @param dingDan
     * @return
     */
    @Override
    public int getAllCount(DingDan dingDan) {
        StringBuilder sql = new StringBuilder("select count(ddid) from t_dd d\n" +
                "JOIN t_user u\n" +
                "on d.userid = u.userid  where 1=1");
        if (dingDan.getName()!=null && !"".equals(dingDan.getName())){
            sql.append(" and u.name like '%").append(dingDan.getName()).append("%'");
        }
        if (dingDan.getDdbh()!=null && !"".equals(dingDan.getDdbh())){
            sql.append(" and d.ddbh like '%").append(dingDan.getDdbh()).append("%'");
        }
        if (dingDan.getStatus()!=null && !"".equals(dingDan.getStatus())){
            sql.append(" and d.status =").append(dingDan.getStatus());
        }
        if (dingDan.getOnTime()!=null && !"".equals(dingDan.getOnTime())){
            sql.append(" and d.ddate >= '").append(dingDan.getOnTime()).append("'");
        }
        if (dingDan.getOffTime()!=null && !"".equals(dingDan.getOffTime())){
            sql.append(" and d.ddate <= '").append(dingDan.getOffTime()).append("'");
        }
        List<List> lists = JDBCUtil.queryList(sql.toString());
        if(lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }

    /**
     * 查询一条订单详情信息
     * @param dingDan
     * @return
     */
    @Override
    public List<Map<String, Object>> queryOnedd(DingDan dingDan) {
        String sql = "select x.ddxqid,x.ddid,b.bookname,b.author,b.publisher,x.count,x.outprice from t_dd_xq x\n" +
                "JOIN t_book b\n" +
                "on x.bookid = b.bookid\n" +
                "where ddid = ? limit ?,?";
        List<Map<String, Object>> list = JDBCUtil.queryMap(sql, dingDan.getDdid(),(dingDan.getPage()-1)*dingDan.getLimit(),dingDan.getLimit());
        if(list.size()>0){
            return list;
        }
        return null;
    }

    /**
     * 查询一条订单详情信息的条数
     * @param dingDan
     * @return
     */
    @Override
    public int getOneCount(DingDan dingDan) {
        String sql = "select count(*) from t_dd_xq where ddid = ?";
        List<List> lists = JDBCUtil.queryList(sql,dingDan.getDdid());
        if(lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }


    /**
     * 向订单 中 插入 已经付款的 信息
     * @param totalcount
     * @param totalprice
     * @return
     */
    @Override
    public int insertDingDan(String totalcount, String totalprice,Integer userid) {
        String sql = "insert into t_dd values(null,?,?,?,?,?,0)";
        Date date = new Date();
        //获取当前时间
        long time = date.getTime();
        //生成一个 六位数 的 随机数
        int i2 = (int) ((Math.random() * 9 + 1) * 100000);
        String s = String.valueOf(i2);
        String ddbh = time + s;

        int i = JDBCUtil.getPrimaryKey(sql, new Date(), userid, totalprice, totalcount, ddbh);
        return i;
    }

    /**
     * 向 订单详情 表中 添加 订单详情
     * @param maps
     * @param ddid
     */
    @Override
    public void insertDingDanXQ(List<Map<String, Object>> maps, int ddid) {
        for (Map<String, Object> map : maps) {
            String sql = "insert into t_dd_xq values(null,?,?,?,?)";
            int i = JDBCUtil.update(sql, map.get("bookid"), map.get("count"), ddid, map.get("outprice"));
        }
    }

    /**
     * 根据 该用户的 userid  查询 该用户的 ddid
     * @param userid
     * @return
     */
    @Override
    public List<Map<String,Object>> getOneDDidByUserid(Integer userid) {
        String sql = "select ddid from t_dd where userid = ? ORDER BY ddate DESC,ddbh DESC";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, userid);
        if (maps.size() >0){
            return maps;
        }
        return null;
    }

    /**
     * 根据查询到的 ddid 查询 该订单的 订单详情
     * @param maps
     * @return
     */
    @Override
    public List<Map<String, Object>> getDingDanXQByddid(List<Map<String,Object>> maps) {
        ArrayList<Map<String, Object>> bigmaps = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            String sql = "select tdx.ddxqid,tdx.bookid,tb.bookname,tb.url,tdx.ddid,td.ddbh,td.`status`,tdx.count,tdx.outprice,td.ddate from t_book tb INNER JOIN t_dd_xq tdx ON tb.bookid = tdx.bookid INNER JOIN t_dd td ON td.ddid =tdx.ddid where td.ddid = ?";
            List<Map<String, Object>> maps1 = JDBCUtil.queryMap(sql, map.get("ddid"));
            //System.out.println(maps1);
            if (maps1.size()>0){
                for (Map<String, Object> stringObjectMap : maps1) {
                    bigmaps.add(stringObjectMap);
                }
            }
        }
        return bigmaps;
    }
    /**
     * 修改订单状态
     * @param ddid
     * @return
     */
    @Override
    public int updatestatus(Integer ddid) {
        String sql = "update t_dd set status = 1 where ddid=?";
        int update = JDBCUtil.update(sql, ddid);
        return update;
    }
    /**
     * 获取订单详情
     * @param ddid
     * @return
     */
    @Override
    public List<Map<String, Object>> getddxq(Integer ddid) {
        String sql = "select bookid,count from t_dd_xq where ddid = ?";
        List<Map<String, Object>> list = JDBCUtil.queryMap(sql, ddid);
        return list;
    }
    /**
     * 获取相应书籍的原有数量
     * @param bookid
     * @return
     */
    @Override
    public int getBookCount(Integer bookid) {
        String sql = "select count from t_book where bookid = ?";
        List<List> lists = JDBCUtil.queryList(sql, bookid);
        if(lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }
    /**
     * 修改相应书籍的数量
     * @param count
     * @param bookid
     * @return
     */
    @Override
    public int updateBookCount(Integer count, Integer bookid) {
        String sql = "update t_book set count = ? where bookid = ?";
        int update = JDBCUtil.update(sql,count,bookid);
        return update;
    }
}
